import { selectAllModel, getFieldList } from '@/api/model'
const state = {
  currentCategory: {}, // 当前栏目(前台预览内容详情的时候需要知道栏目的模板id，由此获取)
  categoryCrumb: [], // 当前栏目路径面包屑
  modelList: [], // 模型列表
  fieldList: [], // 当前模型的字段列表
  singleActionUrl: process.env.VUE_APP_SERVICE_BASE + '/file/sysFile/upload',
  splitActionUrl: process.env.VUE_APP_SERVICE_BASE + '/file/sysFile/uploadChunk',
  tableType: 'table', //表格样式(table: 表格样式，card: 卡片样式)default card
  isScroll: false,
  clip_port: 10086, // 粘贴插件端口
  pluginConfig: {
    version: '',
    downloadLink: '//api.my120.org/锐狐粘贴插件v3.1.zip',
    isUse: false
  }
}

const mutations = {
  SET_CURRENT_CATEGORY: (state, value) => {
    state.currentCategory = value
  },
  SET_CATEGORY_CRUMB: (state, value) => {
    state.categoryCrumb = value
  },
  SET_MODEL_LIST: (state, modelList) => {
    state.modelList = [...modelList]
  },
  SET_FIELD_LIST: (state, fieldList) => {
    state.fieldList = [...fieldList]
  },
  CHANGE_TABLE_STYLE: (state, type) => {
    let _type = state.tableType === 'table' ? 'card' : 'table'
    if (type) _type = type
    state.tableType = _type
  },
  SET_ISSCROLL: (state, isScroll) => {
    state.isScroll = isScroll
  }
}

const actions = {
  // 更新当前栏目
  updateCategoryCrumb(context, id) {
    return new Promise((resolve) => {
      let categoryCrumb = [], categoryFlat = context.rootState.category.categoryFlat
      if (id && context.rootState.category.categoryFlat.length > 0) {
        let category = categoryFlat.find(item => item.id == id)
        if (category && category.parentString) {
          let pString = JSON.parse(category.parentString)
          pString.push(category.id)
          // 查找栏目树
          pString.forEach(item => {
            let _category = context.rootState.category.categoryFlat.find(ele => ele.id == item)
            if (_category) {
              categoryCrumb.push(_category.name)
            } else {
              categoryCrumb.push('首页')
            }
          })
        }
      }
      context.commit('SET_CATEGORY_CRUMB', categoryCrumb)
      resolve()
    })
  },
  // 更新模型列表
  updateModelList({ commit }) {
    return new Promise((resolve, reject) => {
      selectAllModel().then(res => {
        commit('SET_MODEL_LIST', res.data)
        if (res.code) resolve(res.data)
        else reject('error')
      })
    })
  },
  // 更新模型字段列表
  updateFieldList({ commit }, modelId) {
    return new Promise((resolve, reject) => {
      getFieldList({ modelId, enable: true }).then(res => {
        commit('SET_FIELD_LIST', res.data)
        if (res.code) resolve(res.data)
        else reject('error')
      })
    })
  },
  // 更新上传url地址
  updateUploadUrl() {
    return new Promise(resolve => {
      resolve('http://10.10.0.71:9109/file/sysFile/upload')
    })
  },
  // 更新视频上传地址
  updateUploadVideoUrl() {
    return new Promise(resolve => {
      resolve('http://10.10.0.71:9109/file/sysFile/upload')
    })
  }
}

export default {
  namespaced: true, //命名空间
  state,
  mutations,
  actions
}
